﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Microsoft.AspNet.Identity;

namespace GPS.Controllers.GPS_NghiepVu
{
    public class GPS_DKH_GVController : Controller
    {

        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();

            string userId = User.Identity.GetUserId();

            ApplicationUser _currentUser = dbUser.Users.Find(userId);

            return _currentUser;
        }
        //
        // GET: //
        public ActionResult Index(ViewModelDKH_GV SearchModel)
        {
            initialCategorySearchAction(SearchModel);
            var entities = _db.Filter<DKH_GV>(o => (SearchModel.KH_GV_DVGTVT_MA == null || o.KH_GV_DVGTVT_MA == SearchModel.KH_GV_DVGTVT_MA)
                                                             && (SearchModel.KH_GV_KH_MA == null || o.KH_GV_GV_MA.Contains(SearchModel.KH_GV_GV_MA))
                                                             && (SearchModel.KH_GV_KH_MA == null || o.KH_GV_KH_MA == SearchModel.KH_GV_KH_MA)
                                                             );

            var model = new List<DKH_GV>();

            foreach (var currentEntity in entities)
            {
                model.Add(Mapper.Map<DKH_GV>(currentEntity));
            }

            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);

            ViewBag.TotalRow = entities.Count();

            return View(SearchModel);
        }


        #region Create
        public ActionResult Create()
        {
            var model = new CrudModelDKH_GV();
            initialCategoryCreateAction();

            return View(model);
        }

        [HttpPost]
        public ActionResult Create(CrudModelDKH_GV model)
        {
            if (ModelState.IsValid)
            {
                DKH_GV _model = new DKH_GV();
                GPS.COMMON.Helpers.CopyObject<DKH_GV>(model, ref _model);

                _model.ID = EFSequence.GetNextValue("SEQ_DKH_GV_ID");
               
                _model.KH_GV_NGUOINHAP = User.Identity.Name;
                _model.KH_GV_NGAYNHAP = DateTime.Now;


                _db.Insert<DKH_GV>(_model);
                return RedirectToAction("Index");
            }

            return View(model);
        }
     
        #endregion

        #region Edit
        public ActionResult Edit(decimal id)
        {

            DKH_GV model = _db.GetOne<DKH_GV>(o => o.ID == id);

            CrudModelDKH_GV _model = new CrudModelDKH_GV();
            GPS.COMMON.Helpers.CopyObject<CrudModelDKH_GV>(model, ref _model);

        
            initialCategoryEditAction(_model);
            return View(_model);
        }

        [HttpPost]
        public ActionResult Edit(CrudModelDKH_GV model)
        {
            if (ModelState.IsValid)
            {
                var _model = _db.GetOne<DKH_GV>(o => o.ID == model.ID);
                GPS.COMMON.Helpers.CopyObject<DKH_GV>(model, ref _model, new List<string> { "KH_GV_NGUOINHAP", "KH_GV_NGAYNHAP" });
              
                _model.KH_GV_NGUOISUA = User.Identity.Name;
                _model.KH_GV_NGAYSUA = DateTime.Now;

                _db.Update<DKH_GV>(_model);
                return RedirectToAction("Index");
            }

            return View(model);
        }


        #endregion

        #region Delete
        public ActionResult Delete(decimal id)
        {

            DKH_GV model = _db.GetOne<DKH_GV>(o => o.ID == id);
            _db.DeleteItem<DKH_GV>(model);
            return RedirectToAction("Index");
        }
        #endregion

        #region"Ajax"
        [HttpPost]
        public ActionResult getKH_MAs(string ma_DVGT)
        {
            var districts = _db.Filter<DKH>(d => d.KH_DVGTVT_MA == ma_DVGT).Select(a => "<option value='" + a.KH_MA + "'>" + a.KH_TEN + "</option>");

            return Content(string.Join("", districts));
        }

        [HttpPost]
        public ActionResult getGV_MAs(string Ma_DVGT, string ma_LOP)
        {
            var districts = _db.Filter<DGV>(d => d.GV_DVGTVT_MA == Ma_DVGT).Select(a => "<option value='" + a.GV_MA + "'>" + a.GV_HODEM + " " + a.GV_TEN + "</option>");

            return Content(string.Join("", districts));
        }

      
        #endregion

        public void initialCategorySearchAction(ViewModelDKH_GV viewModel)
        {
            // ApplicationUser curUser = getCurrentUser();

            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;

            //KH_MAs
            var khoahocs = _db.Filter<DKH>(o => o.KH_DVGTVT_MA == viewModel.KH_GV_DVGTVT_MA);
            ViewBag.KH_MAs = new SelectList(khoahocs, "KH_MA", "KH_TEN");


        }

        public void initialCategoryCreateAction()
        {
            ApplicationUser curUser = getCurrentUser();
            string _madvgtvt = curUser.MA_TINHTP;

            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40" && o.DVGTVT_MA == _madvgtvt), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;
        }

        public void initialCategoryEditAction(CrudModelDKH_GV view)
        {

            // ApplicationUser curUser = getCurrentUser();
            ApplicationUser curUser = getCurrentUser();
            string _madvgtvt = curUser.MA_TINHTP;
            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40" && o.DVGTVT_MA == _madvgtvt), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;


            //KH_MAs
            var khoahocs = _db.Filter<DKH>(o => o.KH_DVGTVT_MA == view.KH_GV_DVGTVT_MA);
            ViewBag.KH_MAs = new SelectList(khoahocs, "KH_MA", "KH_TEN");



            var gvs = _db.Filter<DGV>(o => o.GV_DVGTVT_MA == view.KH_GV_DVGTVT_MA).Select(
                r => new
                {
                    GV_MA = r.GV_MA,
                    GV_TEN = r.GV_HODEM + " " + r.GV_TEN,
                }).OrderBy(r => r.GV_MA).ToList();
            ViewBag.GV_MAs = new SelectList(gvs, "GV_MA", "GV_TEN");


        }
    }
}